package com.learn.twoday;

import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/8
 */
public class SleepTest {

    private static final Logger logger=Logger.getLogger(SleepTest.class);

    /**
     * t线程状态：RUNNABLE--->TIMED_WAITING
     * @param args
     */
    public static void main(String[] args) {
       Thread t= new Thread(
                ()->{
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
        );
       t.start();
       logger.debug("t线程状态："+t.getState());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.debug("t线程状态："+t.getState());
    }

    @org.junit.jupiter.api.Test
    public void testInterrupt(){
        Thread t1=new Thread(
                ()->{
                    logger.info("enter sleep...");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        logger.info("wake up...");
                        e.printStackTrace();
                    }
                }
        );
        t1.start();

        try {
            Thread.sleep(1000);
            logger.info("interrupt.....");
            t1.interrupt();//打断t1线程的sleep,会抛出InterruptedException
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();

            e.printStackTrace();
        }
    }




}
